package com.wjq.randomplay.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wjq.randomplay.R;
import com.wjq.randomplay.adapter.CityListVH;
import com.wjq.randomplay.base.BaseActivity;
import com.wjq.randomplay.base.BaseAdapter;
import com.wjq.randomplay.util.PreferencesUtils;
import com.wjq.randomplay.util.T;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJQ on 2017/2/3.
 */
public class CityListActivity extends BaseActivity implements XRecyclerView.LoadingListener {
    public XRecyclerView mXRcv;
    ArrayList<String> mArrayList;
    @Bind(R.id.et_city)
    EditText mEtCity;

    @Override
    public void initView() {
        super.initView();
        mTvTitle.setText("城市列表");
        mArrayList = new ArrayList<>();
        initRcv();
        initList();
        initAdapter();
    }

    private void initRcv() {
        mXRcv = (XRecyclerView) findViewById(R.id.xrcv);
        mXRcv.setRefreshProgressStyle(ProgressStyle.BallGridPulse);
        mXRcv.setLaodingMoreProgressStyle(ProgressStyle.BallGridPulse);
        mXRcv.setLoadingListener(this);
        mXRcv.setLoadingMoreEnabled(false);
        LinearLayoutManager mLlManager = new LinearLayoutManager(this, LinearLayout.VERTICAL, false);
        mXRcv.setLayoutManager(mLlManager);

    }

    private void initList() {
        mArrayList.clear();
        String str = PreferencesUtils.getPreferences(mContext, PreferencesUtils.CITY_LIST);
        if (TextUtils.isEmpty(str)) {
            return;
        }
        String[] sArray = str.split(",");
        for (int i = 0; i < sArray.length; i++) {
            mArrayList.add(sArray[i]);
        }
    }

    BaseAdapter<String> mAdapter;

    private void initAdapter() {
        mAdapter = new BaseAdapter<>(mArrayList, this);
        mAdapter.setViewHolder(CityListVH.class);
        mXRcv.setAdapter(mAdapter);
    }


    @OnClick(R.id.tv_add)
    public void mAddClick() {
        String cityName = mEtCity.getText().toString().trim();
        String listStr = PreferencesUtils.getPreferences(mContext, PreferencesUtils.CITY_LIST);
        if (TextUtils.isEmpty(cityName)) {
            T.showShort(mContext, "城市名不能为空！");
            return;
        }
        if (TextUtils.isEmpty(listStr)) {//第一条数据
            listStr += cityName;
        } else {
            listStr += "," + cityName;
        }
        if (checkIsExist(cityName)) {
            T.showShort(mContext, "已经存在" + cityName + "这个地方啦！");
            return;
        } else {
            PreferencesUtils.setPreferences(mContext, PreferencesUtils.CITY_LIST, listStr);
            T.showShort(mContext, "添加成功");
            mEtCity.setText("");
            onRefresh();
        }

    }

    private boolean checkIsExist(String cityName) {
        initList();
        boolean flag = false;
        for (int i = 0; i < mArrayList.size(); i++) {
            if (mArrayList.get(i).equals(cityName)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_citylist;
    }

    @Override
    protected void onNetErrorClick() {

    }

    @Override
    public void onRefresh() {
        initList();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMore() {

    }
}
